package cn.kmust.booth.portal.service.impl;



import cn.kmust.booth.portal.mapper.AreaMapper;
import cn.kmust.booth.portal.model.Area;
import cn.kmust.booth.portal.service.IAreaService;
import cn.kmust.booth.portal.service.ex.*;
import cn.kmust.booth.portal.vo.AreaVO;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tedu.cn
 * @since 2020-08-08
 */
@Service

public class AreaServiceImpl implements IAreaService {
    @Autowired
    private AreaMapper areaMapper;


    //查找剩余摊位数量
    @Override
    public Integer findnumber(Integer id) {
        Integer number = areaMapper.findnumber(id);
        return number;
    }

    //查找总摊位数量
    @Override
    public Integer findtotal(Integer id) {
        Integer total = areaMapper.findtotal(id);
        return total;
    }

    //查找已使用摊位数量
    @Override
    public Integer findusenumber(Integer id) {
        Integer integer = areaMapper.findtotal(id)-areaMapper.findnumber(id);
        return integer;
    }

    @Override
    public Area add(AreaVO areaVO, Integer userId, String startTime, String endTime) {
        LocalDateTime nowTime = LocalDateTime.now();
        DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String time = dtf2.format(nowTime);
        //创建Area对象
        //补全area对象的属性值
        Area area = new Area();
        //判断用户id是否是管理员
//        User user = UserMapper.selectById(userId);
//        if(user == null){
//            throw new PermissionDeniedException("您不具备添加摊位的权限!请登录再试");
//        }
        area.setUserId(userId);
        area.setTotal(areaVO.getTotal());
        area.setNumber(areaVO.getNumber());
        area.setLocalInfo(areaVO.getLocalInfo());
        area.setFlag(areaVO.getFlag());
        area.setOpenTime(time + " " + startTime);
        area.setCloseTime(time + " " + endTime);

        //调用areaMapper.insert(area)方法插入地摊区域的数据,并获取返回结果
        Integer rows = areaMapper.insert(area);
        //判断返回值是否不为1
        if (rows != 1) {
            //是:抛出InsertException异常
            throw new InsertException("添加地摊区域失败,请稍后再试!");
        }

        //返回
        return area;
    }

    @Override
    public Area delete(Integer id,Integer userId) {
        //根据参数id调用areaMapper.selectById(id)查询被删除的"地摊区域"的信息
        Area area = areaMapper.selectById(id);
        //判断查询结果是否为null
        if (area == null) {
            //是:该"摊位"不存在,抛出StallNotFoundException异常
            throw new StallNotFoundException("删除地摊区域失败,尝试访问的地摊区域不存在!");
        }
        //基于查询结果中的userId,结合参数userId,判断查询结果过数据是否是管理员的
        if(!area.getUserId().equals(userId)){
            throw new PermissionDeniedException("删除地摊区域失败!仅管理员可以删除此地摊区域!");
        }
        //根据参数id调用areaMapper.deleteById()执行删除,并获取返回的受影响的行数
        Integer rows = areaMapper.deleteById(id);
        //判断返回值是否不为1
        if (rows != 1){
            //是:抛出DeleteException
            throw new DeleteException("删除地摊区域失败,请稍后再进行尝试");
        }
        //返回查询结果
        return area;
    }

    @Override
    public Area update(AreaVO areaVO,Integer id, Integer userId,String startTime,String endTime) {
        LocalDateTime nowTime = LocalDateTime.now();
        DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String time = dtf2.format(nowTime);

        //判断userId是否是管理员
//        User user = UserMapper.selectById(userId);
//        if(user == null){
//            throw new PermissionDeniedException("您不具备添加地摊区域的权限!请登录再试");
//        }
        //根据参数id调用areaMapper.selectById(id)查询被删除的"地摊区域"的信息
        Area area = areaMapper.selectById(id);
        //判断查询结果是否为null
        if (area == null) {
            //是:该"摊位"不存在,抛出StallNotFoundException异常
            throw new StallNotFoundException("修改地摊区域信息失败,尝试访问的地摊区域不存在!");
        }

        //创建新的Area对象
        //将id,userId封装到area中
        area = new Area()
                .setId(id)
                .setUserId(userId)
                .setTotal(areaVO.getTotal())
                .setNumber(areaVO.getNumber())
                .setLocalInfo(areaVO.getLocalInfo())
                .setFlag(areaVO.getFlag())
                .setOpenTime(time + " " + startTime)
                .setCloseTime(time + " " + endTime);

        // 根据area调用areaMapper.updateById()执行修改，并获取返回的受影响行数
        Integer rows = areaMapper.updateById(area);
        // 判断返回值是否不为1
        if(rows!=1){
            // 是：抛出UpdateException
            throw new UpdateException("修改失败！服务器繁忙请稍后再试！");
        }

        return area;
    }
}
